package com.xstech.library.rxjavahttp;

import java.io.IOException;
import java.util.List;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class MoreBaseUrlInterceptor implements Interceptor {
    //    public static boolean dev_prod = true;
//    public static String baseUrl = "";

    @Override
    public Response intercept(Chain chain) throws IOException {
        //获取原始的originalRequest
        Request originalRequest = chain.request();
        //获取老的url
        HttpUrl oldUrl = originalRequest.url();
        //获取originalRequest的创建者builder
        Request.Builder builder = originalRequest.newBuilder();
        //获取头信息的集合如：manage,mdffx
        List<String> urlnameList = originalRequest.headers("urlName");
        HttpUrl baseURL = null;
        if (urlnameList != null && urlnameList.size() > 0) {
            //删除原有配置中的值,就是namesAndValues集合里的值
            builder.removeHeader("urlName");
            //获取头信息中配置的value,如：manage或者mdffx
            String urlname = urlnameList.get(0);
            //根据头信息中配置的value,来匹配新的base_url地址
            if ("center".equals(urlname)) {
                //这里根据业务 在登录页面设置
                baseURL = HttpUrl.parse(ApiAddress.api_center);
            } else if ("map".equals(urlname)) {
                baseURL = HttpUrl.parse(ApiAddress.api_map);
            } else if ("push".equals(urlname)) {
                baseURL = HttpUrl.parse(ApiAddress.api_push);
            } else if ("mobilePoint".equals(urlname)) {
                baseURL = HttpUrl.parse(ApiAddress.api_mobilePoint);
            }else if ("monitor".equals(urlname)) {
                baseURL = HttpUrl.parse(ApiAddress.apiMonitor);
            }else if ("ota".equals(urlname)) {
                baseURL = HttpUrl.parse(ApiAddress.api_ota);
            }
        }

        //重建新的HttpUrl，需要重新设置的url部分
        HttpUrl newHttpUrl = oldUrl.newBuilder()
                .scheme(baseURL.scheme())//http协议如：http或者https
                .host(baseURL.host())//主机地址
                .port(baseURL.port())//端口
                .build();

        //获取处理后的新newRequest
        Request newRequest = builder.url(newHttpUrl).build();
        return chain.proceed(newRequest);


    }
}
